﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace PTTPOS_Solution.DAL
{
   public static class Discounts_DA
    {

       public static void UpdateDiscount(string id, string Rebateform, string Value)
       {
           MySqlCommand com = new MySqlCommand("update tbldiscounts set Rebateform=@Rebateform, Value=@Value where id=@id", Connection_PTTPOS.GetConnection());
           com.Parameters.AddWithValue("@id", id);
           com.Parameters.AddWithValue("@Rebateform", Rebateform);
           com.Parameters.AddWithValue("@Value", Value);
           com.Connection.Open();
           com.ExecuteNonQuery();
           com.Connection.Close();
       }
       public static DataTable listDiscount(string id)
       {
           DataTable tbl = new DataTable();
           MySqlCommand com = new MySqlCommand("select * from tbldiscounts where id=@id", Connection_PTTPOS.GetConnection());
           com.Parameters.AddWithValue("@id", id);
           com.Connection.Open();
           MySqlDataAdapter adapter = new MySqlDataAdapter(com);
           adapter.Fill(tbl);
           com.Connection.Close();
           return tbl;
       }
       public static void InsertDiscount(string Rebateform, string Value)
       {
           MySqlCommand com = new MySqlCommand("insert into tbldiscounts(id,Rebateform,Value) values(null, @Rebateform, @Value)", Connection_PTTPOS.GetConnection());
           com.Parameters.AddWithValue("@Rebateform", Rebateform);
           com.Parameters.AddWithValue("@Value", Value);
           com.Connection.Open();
           com.ExecuteNonQuery();
           com.Connection.Close();
       }
       public static void DeleteDiscount(string id_order) // Phương thức Hủy hóa đơn
       {
           MySqlCommand command = new MySqlCommand("delete from tbldiscounts where id=@id_order", Connection_PTTPOS.GetConnection());
           command.Parameters.AddWithValue("@id_order", id_order);
           command.Connection.Open();
           command.ExecuteNonQuery();
           command.Connection.Close();
       }
       public static string GetMaxDiscount()
       {
           MySqlCommand com = new MySqlCommand("select max(id) as id from tbldiscounts", Connection_PTTPOS.GetConnection());
           try
           {
               com.Connection.Open();
               MySqlDataReader dr = com.ExecuteReader();
               if (dr.Read())
                   return dr["id"].ToString();
               else
                   return "";
           }
           finally
           {
               com.Connection.Close();
           }
       }
    }
}
